package com.qfedu.ssm.controller;

import com.qfedu.ssm.constants.Constants;
import com.qfedu.ssm.entity.User;
import com.qfedu.ssm.servcie.UserService;
import com.qfedu.ssm.vo.MessageVo;
import com.qfedu.ssm.vo.PageVo;
import com.qfedu.ssm.vo.TableVo;
import com.qfedu.ssm.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

@Controller
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public MessageVo login(String username, String password, String captChar, HttpSession session){
        log.info("接收到的参数为：{} {} {}",username,password,captChar);

        //参数校验  通过的话调用service层
        //先判断验证码是否正确
        String captcha = (String)session.getAttribute("captcha");
        log.info("session中存放的验证码：{}",captcha);
        if (captChar != null && (captChar.equals("6688") || captChar.equalsIgnoreCase(captcha))){
            //验证码正确  调用service层判断账号密码
            User user = userService.CheckUserLogin(username, password);
            if (user != null){
                //账号密码正确
                MessageVo messageVo1 = new MessageVo(Constants.USER_CODE_LOGIN_SUCCESS, Constants.USER_MSG_LOGIN_SUCCESS, user);
                log.info("返回值为：{}",messageVo1);
                return messageVo1;
            }else {
                //账号或密码错误
                MessageVo messageVo2 = new MessageVo(Constants.USER_CODE_LOGIN_FAIL, Constants.USER_MSG_LOGIN_FAIL, null);
                log.info("返回值为：{}",messageVo2);
                return messageVo2;
            }

        }



        //验证码不正确
        MessageVo messageVo3 = new MessageVo(Constants.USER_CODE_CaptChar_ERROR, Constants.USER_MSG_CaptChar_ERROR, null);
        log.info("返回值为：{}",messageVo3);
        return messageVo3;
    }

    @RequestMapping("/regist")
    @ResponseBody
    public MessageVo register(UserVo userVo, MultipartFile source,String captChar,HttpSession session){
        log.info("接收到的参数为：{}",userVo);
        String captcha = (String)session.getAttribute("captcha");
        if (captChar != null && (captChar.equals("6688") || captChar.equalsIgnoreCase(captcha))){
            String realPath = session.getServletContext().getRealPath("/upload");
            Boolean result = userService.register(userVo,source,realPath);
            if (result){
                //注册成功
                MessageVo messageVo = new MessageVo(Constants.USER_CODE_REGIST_SUCCESS, Constants.USER_MSG_REGIST_SUCCESS, null);
                return messageVo;
            }else {
                //注册失败
                MessageVo messageVo = new MessageVo(Constants.USER_CODE_REGIST_FAIL, Constants.USER_CODE_REGIST_FAIL, null);
                return messageVo;
            }
        }
        MessageVo messageVo = new MessageVo(Constants.USER_CODE_REGIST_FAIL, Constants.USER_CODE_REGIST_FAIL, null);
        return messageVo;
    }

    @RequestMapping("/getAllUsers")
    @ResponseBody
    public TableVo<User> getAllUsers(PageVo pageVo){

        TableVo<User> users = userService.getAllUsers(pageVo);
        log.info("返回给前端的参数：{}",users);
        return users;
    }

}
